package com.lins.framework.ui.home.rxbus;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lins.framework.R;
import com.lins.framework.mvpframe.rx.RxBus;
import com.lins.framework.mvpframe.rx.RxSchedulerHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by Admin on 2017/4/10.
 */

public class RxBusFragment extends Fragment {


    @BindView(R.id.id_tv)
    TextView idTv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rxbus, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getRxBusMsg();
    }

    public void getRxBusMsg() {
        RxBus.$().<User>register(1)
                .compose(RxSchedulerHelper.<User>io_main())
                .subscribe(new Action1<User>() {
                    @Override
                    public void call(User user) {
                        idTv.setText(user.getUsername());
                    }
                });
    }
}
